#ifndef _ROUTE_H_
#define _ROUTE_H_

#include "../define/define.h"
#include "../trans_server/node.h"
#include "../util/cfgparse.h"
#include "../util/log.h"
#include "protocol.h"

class route
{
public:
	route()
	{
		m_log = new logfile("route");

		if(!load_cfg())
			return ;

		if(!parse_cfg())
			return;

		return;
	}

	~route()
	{
		delete m_log;
	}

	bool reload()
	{
	    if(!load_cfg())
			return false;

		if(!parse_cfg())
			return false;

		return true;
	}
	
	bool get_all_task(ip_node* task, uint32 count)
	{
		int i;
		for(i=0;i<128;i++)
		{
			if(m_node[i].d_ip == 0)
				break;

			if(count < i)
				break;

			memcpy(task + i,&m_node[i],sizeof(ip_node));
		}

		return true;
	}

private:
	
	bool load_cfg()
	{
		char tmp_path[1023] = {0};
        GetExePath(tmp_path);
		strcat(tmp_path, "/router.cfg");
 		m_log->WriteTrace(tmp_path);
		return m_cfg.Init(tmp_path);
	}

	bool parse_cfg()
	{
		char tempchar[1024]= {0};
		string temp_str = "";
		temp_str =m_cfg.GetValue(temp_str,"my_port","trans_port");
        G_Port = atoi(temp_str.c_str());


		temp_str = "";
		temp_str = m_cfg.GetValue(temp_str,"Version","trans_info");
        G_Ver  = atoi(temp_str.c_str());

		bool finish = true;
		uint32 i = 1;
		string tmp_str = "task";
		while(finish == true)
		{
			if(i > 128)
			{
				m_log->WriteTrace("error");
				return false;
			}

			tmp_str = "task";
			sprintf(tempchar,"%d",i);
			tmp_str.append(tempchar); 

			temp_str = "";
			temp_str = m_cfg.GetValue(temp_str,"d_ip",tmp_str.c_str());
			if(temp_str.size() == 0)
				break;
			
			uint32 d_ip = inet_addr(temp_str.c_str());
			m_node[i-1].d_ip = d_ip;

			temp_str = "";
			temp_str = m_cfg.GetValue(temp_str,"l_port",tmp_str.c_str());
			uint16 l_port  = atoi(temp_str.c_str());
			m_node[i-1].l_port = l_port;			

			temp_str = "";
			temp_str = m_cfg.GetValue(temp_str,"d_port",tmp_str.c_str());
			uint16 d_port  = atoi(temp_str.c_str());
			m_node[i-1].d_port = d_port;	

			bool task_finish = true;
			uint32 j = 1;
			while(task_finish == true)
			{
				if(j>8)
				{
					m_log->WriteTrace("error");
					return false;
				}

				string ip_str = "ip";
				memset(tempchar,0,1024);
				sprintf(tempchar,"%d",j);
				ip_str.append(tempchar); 
                
				temp_str = "";
				temp_str = m_cfg.GetValue(temp_str,ip_str.c_str(),tmp_str.c_str());

                if (temp_str.size() == 0)
				    break;
				
				parse_ip(temp_str,j-1,&m_node[i-1]);
				m_node[i-1].count = j;
				j++;
			}

			i++;
		}

        return false;
	}

	bool parse_ip(string ip, uint32 index,ip_node* node)
	{
		if(index > 8)
			return false;

		string tmp_str,tmp_str2;
		tmp_str2 = ip;

		uint32 pos = 1;
		uint32 i = 0;

		pos = tmp_str2.find_first_of(" ");
		if(pos != -1)
           tmp_str2 = tmp_str2.substr(0,pos);


		while(pos != -1)
		{
		    if(i> 8)
				return false;
			
			pos = tmp_str2.find_first_of("-");
			if(pos == -1)
			{
			    tmp_str = tmp_str2.substr(0,pos);
				node->trans_link[index].trans_ip[i] = inet_addr(tmp_str.c_str());
				node->trans_link[index].count = i + 1;
 				break;
			}
			else
		       tmp_str = tmp_str2.substr(0,pos);
		    
			node->trans_link[index].trans_ip[i] = inet_addr(tmp_str.c_str());
			
		    tmp_str2 = tmp_str2.substr(pos + 1, tmp_str2.length());
			i++;
        }
    
		return true;
	}


	ip_node       m_node[128];
    
	CfgParse      m_cfg;
    logfile*      m_log;
};
#endif
